package com.tdzh.maimaiti.biz;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.R;
import android.app.Activity;
import android.content.ContentResolver;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.provider.ContactsContract;

import java.util.Objects;

/**
 * Created by hisou on 2016/4/20.
 */
public class GetContractList {

    public static String GetContractListString(ContentResolver cr,String userphone) {
        //ContentResolver cr = getContentResolver();
        Cursor cursor = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
        //SharedPreferences user = getSharedPreferences("account",0);
       // String phone = user.getString("name", null);
        String phoneList = userphone;
        //phoneList += "联系人列表:";
        while (cursor.moveToNext()) {
            int nameFieldColumnIndex = cursor.getColumnIndex(ContactsContract.PhoneLookup.DISPLAY_NAME);

            //String name = cursor.getString(nameFieldColumnIndex);
            //System.out.println("阿瓦打我的娃");

            String ContactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
            Cursor phone = cr.query(
                    ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
                    ContactsContract.CommonDataKinds.Phone.CONTACT_ID + "=" + ContactId, null, null);
            while (phone.moveToNext())

            {
                String number = phone.getString(phone.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                System.out.println("联系人获取中");
                String realNumber = number.replace(" ", "");
                if (realNumber.length() != 11) {
                    System.out.println("长度不匹配" + realNumber);
                    continue;
                }
                phoneList += realNumber;


            }

        }
        cursor.close();
        return phoneList;
    }

    public static List<Map<String, Object>> resolveContractsList(String arr) throws JSONException {


        List<Map<String, Object>> mlists = new ArrayList<Map<String, Object>>();

        //byte[] data = arr;
        if(arr.equals("Success") || arr.equals("Fail")){
            return  null;
        }

        JSONArray array = new JSONArray(arr);

        for (int i = 0; i < array.length(); i++)

        {
            JSONObject item = array.getJSONObject(i);
            Map<String, Object> map = new HashMap<String, Object>();

            map.put("name", item.optString("name"));

            map.put("phoneNumber", item.optString("phoneNumber"));

           // map.put("state", item.getString("state"));

            map.put("image", R.mipmap.sym_def_app_icon);

            //int age = item.getInt("age");
            mlists.add(map);
        }
        return  mlists;
    }
}